require("../util.js");
require("../component/components.js");
require("../../jsMt/util.js");
var userInfo=JSON.parse(sessionStorage.getItem("userInfo"));
if(!userInfo){
    location.href="/loginIndex"
}
var vm = new Vue({
    el:"#address",
    data:{
        name:"",
        type:"",
        addressList:{},
        expansion: null,
        orderNumber:"",
        pop:{
            question: "同意退货并提交该退货地址？",
            show: false
        },
        addressId:"",
        cartId:""
    },
    mounted:function(){
        this.searchObject = this.getQueryStringArgs();
        this.type=this.searchObject&&this.searchObject.type;
        this.orderNumber=this.searchObject&&this.searchObject.orderNumber;
        this.cartId=this.searchObject&&this.searchObject.cartId;
        if(this.type==1||this.type==3){
            this.name="收货地址";
        }else if(this.type==2){
            this.name="选择退货地址";
        }
        this.getUserDeliveryAddressList();
    },
    methods:{
        //后退
        getBack: function () {
            if(this.type==1){
                location.href="/happyi/setUp";
            }else if(this.type==2){
                location.href="/happyi/refundInfo?type=1&uid="+userInfo.id+"&orderNumber="+this.orderNumber;
            }else if(this.type==3){
                history.go(-1);
            }
        },
        /*获取查询字符对象*/
        getQueryStringArgs: function () {
            //取得查询字符串并去掉开头的问号
            var qs = (location.search.length > 0 ? location.search.substring(1) : ""),
                args = {},
                items = qs.length ? qs.split("&") : [],
                item = null,
                name = null,
                value = null,
                len = items.length;
            for (var i = 0; i < len; i++) {
                item = items[i].split("=");
                name = item[0];
                value = item[1];
                if (name.length) {
                    args[name] = value;
                }
            }
            return args;
        },
        //地址列表
        getUserDeliveryAddressList:function(){
            var that=this;
            var data={
                loginUid:userInfo.id
                // loginUid:1
            };
            var url;
            if(that.type==1||that.type==3){
                url="/user/getUserDeliveryAddressList"
            }else if(that.type==2){
                url="/happyi/getUserReturnAddressList"
            }
            this.ajax({url:url,data:data,dataType:"json",method:"post"},function(b){
                if(b.json.code==0&&b.json.data){
                    that.addressList=b.json.data;
                }else{
                    vm.$refs.prompt.toast("获取地址列表信息失败！")
                }
            });
        },
        //删除地址
        deleteUserDeliveryAddress:function(id){
            var that=this;
            var data={
                loginUid:userInfo.id,
                addressId:id
            };
            console.log(data)
            this.ajax({url:"/user/deleteUserDeliveryAddress",data:data,dataType:"json",method:"post"},function(b){
                if(b.json.code==0){
                    vm.$refs.prompt.toast("删除成功！");
                    if($(".address-listLeft").hasClass("swipeleft")){
                        $(".address-listLeft").removeClass("swipeleft");
                    }
                    that.getUserDeliveryAddressList();
                }else{
                    vm.$refs.prompt.toast("删除失败,必须保留一个地址！")
                }
            });
        },
        //退货显示框
        getIsClassify:function(addressId){
            console.log("ddddd")
            this.pop.show=!this.pop.show;
            this.addressId=addressId;
        },
        //同意退货
        agreeReturnGoods:function(){
            var that=this;
            var data={
                loginUid:that.loginUid,
                orderNumber:that.orderNumber,
                addressId:that.addressId
            };
            console.log(data);
            this.ajax({url:"/happyi/agreeReturnGoods",data:data,dataType:"json",method:"post"},function(b){
                if(b.json.code==0){
                    that.getBack();
                    that.getIsClassify();
                }else{
                    vm.$refs.prompt.toast("退货失败！");
                }
            });
        },
        getNext:function(num,addressId){
            var path;
            if(num==1){
                path="/addAddress?type=1&uid="+userInfo.id;
            }else if(num==2){
                if(this.type==1){
                    path="/addAddress?type=2&uid="+userInfo.id+"&addressId="+addressId;
                }else if(this.type==2){
                    path="/addAddress?type=5&uid="+userInfo.id+"&addressId="+addressId;
                }else if(this.type==3){
                    path="/addAddress?type=3&uid="+userInfo.id+"&addressId="+addressId;
                }
            }else if(num==3){
                path="/addAddress?type=4&uid="+userInfo.id;
            }else if(num==4){
                if(this.type==3){
                    path="/purchase/confirmOrder?uid="+userInfo.id+"&cartId="+this.cartId+"&addressId="+addressId;
                }else{
                    return false;
                }
            }
            location.href=path;
        }
    },
    watch:{
        addressList:function(){
            this.$nextTick(function(){
                var that=this;
                var container = document.querySelectorAll('.address-cent .address-listLeft');
                console.log(container)
                var expansion = null; //是否存在展开的list
                for(var i = 0; i < container.length; i++){
                    var x, y, X, Y, swipeX, swipeY;
                    container[i].addEventListener('touchstart', function(event) {
                        x = event.changedTouches[0].pageX;
                        y = event.changedTouches[0].pageY;
                        swipeX = true;
                        swipeY = true ;
                        if(expansion){   //判断是否展开，如果展开则收起
                            expansion.className ="address-listLeft";
                        }
                    });
                    container[i].addEventListener('touchmove', function(event){
                        event.stopPropagation();
                        X = event.changedTouches[0].pageX;
                        Y = event.changedTouches[0].pageY;
                        // 左右滑动
                        if(swipeX && Math.abs(X - x) - Math.abs(Y - y) > 0){
                            // 阻止事件冒泡
                            event.stopPropagation();
                            if(X - x > 10){   //右滑
                                event.preventDefault();
                                this.className = "address-listLeft";    //右滑收起
                                //console.log("右滑")
                            }
                            if(x - X > 10){   //左滑
                                event.preventDefault();
                                this.className = "address-listLeft swipeleft";   //左滑展开
                                //console.log("左滑")
                                expansion = this;
                            }
                            swipeY = false;
                        }
                        // 上下滑动
                        if(swipeY && Math.abs(X - x) - Math.abs(Y - y) < 0) {
                            swipeX = false;
                        }
                    });
                }
            });
        }
    }
});